package com.yidu.labyrinth.service;

import com.yidu.labyrinth.dao.LabyrinthRolesModulesDao;
import com.yidu.labyrinth.main.LabyrinthModuleEntity;
import com.yidu.labyrinth.main.LabyrinthModuleOrRoleEntity;
import com.yidu.labyrinth.main.LabyrinthRoleEntity;
import com.yidu.labyrinth.main.RoleModuleVo;
import com.yidu.labyrinth.vo.LabyrinthVo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class LabyrinthRolesModulesServiceImpl implements LabyrinthRolesModulesService {

    private LabyrinthRolesModulesDao labyrinthRolesModulesDao;


    public LabyrinthRolesModulesDao getLabyrinthRolesModulesDao() {
        return labyrinthRolesModulesDao;
    }

    public void setLabyrinthRolesModulesDao(LabyrinthRolesModulesDao labyrinthRolesModulesDao) {
        this.labyrinthRolesModulesDao = labyrinthRolesModulesDao;
    }


    @Override
    public void deleteAll(LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity) {
        labyrinthRolesModulesDao.deleteAll(labyrinthModuleOrRoleEntity);

    }

    @Override
    public void add(LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity) {

        //labyrinthRolesModulesDao.add(labyrinthModuleOrRoleEntity);

    }

    @Override
    public List<LabyrinthModuleOrRoleEntity> select(LabyrinthModuleOrRoleEntity labyrinthModuleOrRoleEntity) {


        return null;

    }



    @Override
    public List<LabyrinthVo> findAll() {

        System.out.println("弄死你了");
        List<LabyrinthModuleEntity> list = labyrinthRolesModulesDao.findAll();
        System.out.println("弄死你"+list.get(0).getModuleId());
        List<LabyrinthVo> listVo = new ArrayList<LabyrinthVo>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            LabyrinthModuleEntity module = (LabyrinthModuleEntity) iterator.next();
            LabyrinthVo moduleVo = new LabyrinthVo();

            BeanUtils.copyProperties(module, moduleVo);

            listVo.add(moduleVo);

            System.out.println(moduleVo.toString()+"集合");
        }

        return listVo;




    }

    @Override
    public List<RoleModuleVo> findRoleId(String roleId) {

        List<LabyrinthModuleOrRoleEntity> list = labyrinthRolesModulesDao.findRoleId(roleId);
        List<RoleModuleVo> listVo = new ArrayList<RoleModuleVo>();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            LabyrinthModuleOrRoleEntity roleModule = (LabyrinthModuleOrRoleEntity) iterator.next();
            RoleModuleVo roleModuleVo = new RoleModuleVo();
            BeanUtils.copyProperties(roleModule, roleModuleVo);
            roleModuleVo.setModModuleId(roleModule.getModuleId().getModuleIds());
            roleModuleVo.setModuleId(roleModule.getModuleId().getModuleId());
            roleModuleVo.setModuleName(roleModule.getModuleId().getModuleName());
            //roleModuleVo.setModuleAddress(roleModule.getModuleId().getModuleRemark());
            listVo.add(roleModuleVo);
        }
        return listVo;
    }


    @Override
    public void add(LabyrinthVo rolevo) {



        LabyrinthRoleEntity po = new LabyrinthRoleEntity();

        /*if(rolevo.getModuleResult()==null) {
            rolevo.setRoleValidity("0");
        }
        else if(rolevo.getRoleValidity().equals("on")) {
            rolevo.setRoleValidity("1");
        }else {
            rolevo.setRoleValidity("0");
        }*/


        rolevo.setRoleResult(1);

        rolevo.setRoleCreateDate("2019-10-10");



        BeanUtils.copyProperties(rolevo, po);

        po.setRoleId(UUID.randomUUID().toString().replaceAll("-",""));

        labyrinthRolesModulesDao.add(po);



        if(rolevo.getModuleId()!=null &&!"".equals(rolevo.getModuleId())) {
            System.out.println(rolevo.getModuleId()+"模块id");
            String[] id = rolevo.getModuleId().split(",");
            for (int i = 0; i < id.length; i++) {
                LabyrinthModuleEntity module = labyrinthRolesModulesDao.findById(id[i]);
                LabyrinthModuleOrRoleEntity roleModule = new LabyrinthModuleOrRoleEntity();
                roleModule.setModuleOrRoleId(UUID.randomUUID().toString().replaceAll("-",""));
                roleModule.setModuleId(module);
                roleModule.setRoleId(po);
                labyrinthRolesModulesDao.saveOrUpdate(roleModule);
            }
        }

    }

    @Override
    public void update(LabyrinthVo rolevo) {

        LabyrinthRoleEntity po = new LabyrinthRoleEntity();

      /*  if(rolevo.getRoleValidity()==null) {
            rolevo.setRoleValidity("0");
        }
        else if(rolevo.getRoleValidity().equals("on")) {
            rolevo.setRoleValidity("1");
        }else {
            rolevo.setRoleValidity("0");
        }*/

        BeanUtils.copyProperties(rolevo, po);
        labyrinthRolesModulesDao.update(po);
        if(rolevo.getRoleId()!=null && !"".equals(rolevo.getRoleId())) {
            List<LabyrinthModuleOrRoleEntity> list = labyrinthRolesModulesDao.findRoleId(rolevo.getRoleId());
            for (int i = 0; i < list.size(); i++) {
                //labyrinthRolesModulesDao.delete(list.get(i).getRoleModuleId());
            }
        }
        if(rolevo.getModuleId()!=null &&!"".equals(rolevo.getModuleId())) {
            String[] id = rolevo.getModuleId().split(",");
            for (int i = 0; i < id.length; i++) {
                LabyrinthModuleEntity module = labyrinthRolesModulesDao.findById(id[i]);
                LabyrinthModuleOrRoleEntity roleModule = new LabyrinthModuleOrRoleEntity();
                roleModule.setModuleId(module);
               roleModule.setRoleId(po);
                labyrinthRolesModulesDao.saveOrUpdate(roleModule);
            }
        }


    }




    @Override
    public LabyrinthVo findId(String roleId) {

        LabyrinthRoleEntity po = labyrinthRolesModulesDao.findId(roleId);

        System.out.println(po.getRoleRemark()+"备注");

        LabyrinthVo vo = new LabyrinthVo();

        if(vo.getRoleId()!=null) {

            vo.getRoleId();
        }
        BeanUtils.copyProperties(po, vo);


        return vo;



    }

}
